{% extends 'envs_python/layout_envs_python.html' %}
{% load static %}

{% block panel_css_files %}
<link rel="stylesheet" href="{% static 'plugins/bootstrap-switch/css/bootstrap3/bootstrap-switch.css' %}">
{% endblock %}

{% block panel_js_files %}
<script src="{% static 'plugins/bootstrap-switch/js/bootstrap-switch.min.js' %}"></script>
{% endblock %}

{% block html_js_code %}
{{ block.super }}
<script>
layui.use(function(){
  var $ = layui.$;
  var layer = layui.layer;
  var util = layui.util;

      $("input[data-bootstrap-switch]").each(function () {
          $(this).bootstrapSwitch('state', $(this).prop('checked'))
      })

function parseXHRError(xhr) {
    let message = '未知错误';

    try {
        const response = JSON.parse(xhr.responseText);
        if (response && response.message) {
            message = response.message;
        } else if (xhr.statusText) {
            message = xhr.statusText;
        }
    } catch (e) {
        if (xhr.statusText) {
            message = xhr.statusText;
        }
        console.error('无法解析错误响应:', xhr.responseText);
    }

    return message;
}

function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

  util.on('lay-on', {
    'iframe_select_folder': function(){
      layer.open({
        title:"选择文件/文件夹",
        type: 2,
        area: ['800px', '600px'],
        content: '{% url 'sharedkit:picker_file_dir' 'dir' %}?path={% if install_folder %}{{ install_folder }}{% endif %}',
        fixed: false, // 不固定
        maxmin: true,
        shade: 0.5,
        shadeClose: false,
        btn: ['选中', '取消'],
        btnAlign: 'c',
        yes: function(index, layero){
          var iframeWin =  window[layero.find('iframe')[0]['name']];
          var elemMark = iframeWin.$('#picker_path'); // 获得 iframe 中某个输入框元素
          var value = elemMark.val();
          if($.trim(value) === '') return elemMark.focus();
          $('#id_install_folder').val(value);
          layer.closeAll();
        }
      });
    },
  })
});
</script>
{% endblock %}

{% block page_content %}
{{ block.super }}
{% include 'include_messages.html' %}
<div class="card">
 <form method="post" class="form-horizontal">{% csrf_token %}
    <div class="card-body">
    <div class="form-group row">
      <label class="col-sm-1 col-form-label">{{ form.install_folder.label }}</label>
      <div class="col-sm-11">
          <div class="input-group">
          {{ form.install_folder }}
            <div class="input-group-append">
            <span class="input-group-text hand-cursor" lay-on="iframe_select_folder"><i class="fas fa-folder-open"></i></span>
            </div>
          </div>
          <span class="text-info">{{ form.install_folder.help_text }}</span>
      </div>
    </div>

    <div class="form-group row">
      <label class="col-sm-1 col-form-label">{{ form.install_source.label }}</label>
      <div class="col-sm-5">
          <div class="input-group">
          {{ form.install_source }}
          </div>
          <span class="text-info">{{ form.install_source.help_text }}</span>
      </div>
    </div>

    <div class="form-group row">
      <label class="col-sm-1 col-form-label" for="{{ form.check_file.id_for_label }}">{{ form.check_file.label }}</label>
        <div class="col-sm-5">
             <div class="input-group">
          <div class="icheck-primary d-inline">{{ form.check_file }}</div>
             </div>
             <span class="text-info">{{ form.check_file.help_text }}</span>
        </div>
    </div>

    </div>
     <div class="card-footer">
         <button type="submit" class="btn btn-primary">提交</button>
     </div>
 </form>
</div>
{% endblock %}